// ==UserScript==
// @name         掘金过滤工具
// @namespace    https://github.com/iimondo/juejin
// @version      0.2.1
// @description  过滤指定条目
// @author       iimondo
// @match        https://juejin.cn/*
// @icon         https://lf3-cdn-tos.bytescm.com/obj/static/xitu_juejin_web//static/favicons/favicon-32x32.png
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    console.log("掘金过滤工具 run succesed!");

    let filter_keywords = [
        'offer', '大厂', '面试', '金三银四', '总结', '失业',
        '岁', '裁', '职', '薪',
    ];

    function filter_item(items){
        items.forEach(item => {
            let dom_a = item.querySelector('a.title');
            if(dom_a !== null){
                let item_title_text = dom_a.text;
                let filter_item_result = filter_keywords.filter(keyword =>
                                                                new RegExp(keyword.replaceAll("/","")).test(item_title_text));

                if(Array.isArray(filter_item_result) && filter_item_result.length > 0){
                    console.log(`${filter_item_result} \n delete item: [${item_title_text}]::${dom_a}`);
                    item.remove();
                }
            }
        });
    }

    let mutationObserver = new MutationObserver(function (mutationList, observer) {
        mutationList.forEach((mutation) => {
            switch(mutation.type) {
                case 'childList':
                    if(mutation.addedNodes.length > 0){
                        filter_item(mutation.addedNodes);
                    }
                    break;
            }
        });
    })

    setTimeout(function(){
        try {
            document.querySelector('.app-open-button').remove();
        } catch(error) {

        }

        try {
            document.querySelector('.poster-resouce-wrap').remove();
        } catch(error) {

        }

        try {
            filter_item(document.querySelectorAll('div.entry-list > li'));
        } catch(error) {

        }

        try {
            filter_item(document.querySelectorAll('ul.main-list > li'));
        } catch(error) {

        }

        try {
            let items_1 = document.querySelector('div.entry-list');
            if(items_1 !== null){
                mutationObserver.observe(document.querySelector('div.entry-list'), { attributes: true, childList: true, subtree: false });
            }
        } catch(error) {

        }

        try {
            let items_2 = document.querySelector('ul.main-list');
            if(items_2 !== null){
                mutationObserver.observe(document.querySelector('ul.main-list'), { attributes: true, childList: true, subtree: false });
            }
        } catch(error) {

        }

        console.log('掘金过滤工具 run finish');
    }, 2000);

})();